package com.flyduck.annotation.config;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.flyduck.annotation.interceptor.HeaderInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * <p></p>
 *
 * @author flyduck 2020/10/22 21:53
 * @version V1.0
 * @modify by user: flyduck 2020/10/22
 * @modify by reason:{原因}
 **/
@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Value("${spring.datasource.url}")
    private String dbUrl;

    @Value("${spring.datasource.username}")
    private String dbUsername;

    @Value("${spring.datasource.password}")
    private String dbPassword;

    @Value("${spring.datasource.driver-class-name}")
    private String dbDriver;

    @Autowired
    private HeaderInterceptor headerInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册自定义拦截器
        registry.addInterceptor(headerInterceptor).addPathPatterns("/**");
    }

    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        //配置在反序列化过程中如果json字符串中存在无法匹配的属性不会失败
        mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        //配置禁用日期转换为Timestamp，序列化将调用StdDateFormat的format()方法，使用ISO-8601兼容格式"yyyy-MM-dd'T'HH:mm:ss.SSSZ"
        mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

        //配置序列化禁用empty beans失败的配置
        mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);

        //在序列化时忽略值为 null 的属性
        //mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

        //忽略值为默认值的属性
        //mapper.setDefaultPropertyInclusion(Include.NON_DEFAULT);

        return mapper;
    }

    public String getDbUrl() {
        return dbUrl;
    }

    public void setDbUrl(String dbUrl) {
        this.dbUrl = dbUrl;
    }

    public String getDbUsername() {
        return dbUsername;
    }

    public void setDbUsername(String dbUsername) {
        this.dbUsername = dbUsername;
    }

    public String getDbPassword() {
        return dbPassword;
    }

    public void setDbPassword(String dbPassword) {
        this.dbPassword = dbPassword;
    }

    public String getDbDriver() {
        return dbDriver;
    }

    public void setDbDriver(String dbDriver) {
        this.dbDriver = dbDriver;
    }
}
